<?php
namespace App\Repositories;

use App\Model\MaterialSheetAssign;
use App\Traits\SingletonTrait;
use Hyperf\DbConnection\Db;

class MaterialSheetAssignRepository extends AbstractRepository
{
    use SingletonTrait;

    public function model()
    {
        return 'App\\Model\\MaterialSheetAssign';
    }

    public function getReportFrom($assign_id){
        $assign = MaterialSheetAssignRepository::getInstance()->getById($assign_id);
        $return=null;
        switch ($assign->type){
            case MaterialSheetAssign::TYPE_ONE:
                $return = AreaRepository::getInstance()->one(['id','name'],[[
                    'field' => 'id',
                    'value' => $assign->data_id
                ]]);
                break;
            case MaterialSheetAssign::TYPE_TWO:
                $return = StreetRepository::getInstance()->one(['id','name'],[[
                    'field' => 'id',
                    'value' => $assign->data_id
                ]]);
                break;
            case MaterialSheetAssign::TYPE_THREE:
                $return = CommunityRepository::getInstance()->one(['id','name'],[[
                    'field' => 'id',
                    'value' => $assign->data_id
                ]]);
                break;
        }
        $admin_id_result = MaterialReportRepository::getInstance()->one(['admin_id'],[[
            'field' => 'material_assign_id',
            'value' => $assign_id
        ]]);
        if($admin_id_result){
            $admin_info = AdminInfoRepository::getInstance()->one(['real_name','contact_tel'],[[
                'field' => 'admin_id',
                'value' => $admin_id_result->admin_id
            ]]);
            $contact = $admin_info?$admin_info:new \stdClass();
        }else{
            $contact = new \stdClass();
        }

        return [
            'assign' => $assign,
            'from' => $return,
            'contact' => $contact
        ];
    }
}